import os
import random

from django.utils import simplejson

from google.appengine.ext.webapp import template

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Word(db.Model):
    word = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    
class Verb(Word):
    pass
    
class Adjective(Word):
    pass

class Noun(Word):
    pass    

class MainPage(webapp.RequestHandler):
    def get(self):
    
        verbs_query = Verb.all().order('word')
        verbs = verbs_query.fetch(10)
        
        template_values = {
            'verbs': verbs,
            }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class Guestbook(webapp.RequestHandler):
    def post(self):
        verb = Verb()
        verb.word = self.request.get('content')

        verb.put()
        self.redirect('/')
        
class GenerateSentence(webapp.RequestHandler):
    def get(self):
        verbs_query = Verb.all().order('word')
        verbs = verbs_query.fetch(100)
        
        if verbs:
            verb = random.choice(verbs)
        else:
            verb = 'No words found'
            
            verb = 'Help'
        
        self.response.out.write(simplejson.dumps({'verb':verb.word}))
        
class Test(webapp.RequestHandler):
	def get(self):
		self.response.out.write('Test Succesful')
		


application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Guestbook),
                                      ('/generateSentence', GenerateSentence),
                                      ('/test', Test),],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()